unit script;

interface

uses
  lua52, Sysutils, math,
{$IFDEF fpc}
  LMessages, LConvEncoding, LCLType, LCLIntf, FileUtil,
{$ELSE}
  Windows,
{$ENDIF}
  Classes,
  Engine,
  Main,
  Debug,
  Dinput,
  PakFile,
  Map,
  PicFile,
  Sound,
  Event;

Type

  TScript = Class
    lua_script: Plua_State;
    Game: PGame;
    PakFile: TPakFile;
    Script_Event: PEvent;
    //Engine: PEngine;
    //Input: PInput;
    //sound: Psound;
    //PakFile: PPakFile;
    //picFile: PpicFile;
    constructor Create(PointGame: PGame);
    Destructor Destroy; override;
    function Initial: integer;
    procedure Release;
    function ExecScript(filename, functionname: PChar): integer; overload;
    function ExecScript(Pakname, filename, functionname: PChar): integer; overload;
    function RegisterFunction: integer;
    //function add(L: Plua_state): integer; cdecl;
  End;

  PScript = ^TScript;

var
  GameScript: TScript;

function Script_ReadMusicFromFile(L: Plua_state): integer; cdecl;
function Script_ReadMusicFromPak(L: Plua_state): integer; cdecl;
function Script_PlayMusic(L: Plua_state): integer; cdecl;
function Script_PlayMusicEx(L: Plua_state): integer; cdecl;

implementation

constructor TScript.Create(PointGame: PGame);
begin
  game := PointGame;
  Script_Event := @Game.GameEvent;
  initial;
  PakFile := TPakFile.Create;
end;

Destructor TScript.Destroy;
begin
  Release;
  inherited Destroy;
end;

function TScript.RegisterFunction: integer;
begin
  //注册函数

  //播放音乐
  lua_register(Lua_script, 'ReadMusicFromFile', Script_ReadMusicFromFile); //readmusicFrompak(musicfile): integer;
  lua_register(Lua_script, 'ReadMusicFromPak', Script_ReadMusicFromPak);   //readmusicFrompak(pakfile, musicfile): integer;
  lua_register(Lua_script, 'PlayMusic', Script_PlayMusic);     //playmusic;
  lua_register(Lua_script, 'PlayMusicEx', Script_PlayMusicEx); //playmusicex(x,y,z: integer);

end;

function Script_ReadMusicFromFile(L: Plua_state): integer; cdecl;
var
  MusicName: String;
  FH, len: integer;
begin
  MusicName := Lua_ToString(L, -1);
  if GameScript.Game.GameSound.LoadSoundFromFile(MusicName, Music, 0) = 0 then
    lua_pushnumber(L, 1)
  else
    lua_pushnumber(L, -1);
  {
  if Fileexists(MusicName) then
  begin
    FH := fileopen(MusicName, fmopenread);
    len := fileseek(FH, 0, 2);
    Fileseek(FH, 0, 0);
    //GameScript.game.gameSound.LoadSoundFromFile();
    GameScript.game.gameSound.FModMusicData.datalen := len;
    setlength(GameScript.game.gameSound.FModMusicData.data, GameScript.game.gameSound.FModMusicData.datalen);
    Fileread(FH, GameScript.game.gameSound.FModMusicData.data[0], GameScript.game.gameSound.FModMusicData.datalen);
    Fileclose(FH);
    lua_pushnumber(L, len);
  end
  else
    lua_pushnumber(L, -1); }
  result := 1;
end;

function Script_ReadMusicFromPak(L: Plua_state): integer; cdecl;
var
  PakFileName, MusicName: String;
  len: integer;
begin
  PakFileName := Lua_ToString(L, -2);
  MusicName := Lua_ToString(L, -1);
  len := GameScript.PakFile.ReadFromFileToBuffer(PakFileName, MusicName);
  if len > 0 then
  begin
    GameScript.Game.GameSound.copypakbuftosounddata(@GameScript.PakFile, @GameScript.Game.Gamesound.FModMusicData);
  end;
  lua_pushnumber(L, len);
end;

function Script_PlayMusicEx(L: Plua_state): integer; cdecl;
var
  x, y, z: integer;
begin
  x := floor(lua_tonumber(L, -3));
  y := floor(lua_tonumber(L, -2));
  z := floor(lua_tonumber(L, -1));
  if GameScript.PakFile.bufferlen > 0 then
    GameScript.Game.Gamesound.playmusicEx(x, y, z);
end;

function Script_PlayMusic(L: Plua_state): integer; cdecl;
begin
  if GameScript.PakFile.bufferlen > 0 then
    GameScript.Game.Gamesound.playmusic;
end;

function TScript.Initial: integer;
begin
  Lua_script := luaL_newstate;
  luaL_openlibs(Lua_script);
  luaopen_base(Lua_script);
  luaopen_table(Lua_script);
  luaopen_math(Lua_script);
  luaopen_string(Lua_script);
  RegisterFunction;
end;

procedure TScript.Release;
begin
  lua_close(Lua_script);
  PakFile.Release;
end;

function TScript.ExecScript(filename, functionname: PChar): integer;
var
  Script: AnsiString;
  //Data: string;
  Data: TStringList;
  h: integer;
  len: integer;
begin

  if FileExists(filename) { *Converted from FileExists*  } then
  begin
    h := fileopen(filename, fmopenread);
    len := fileseek(h, 0, 2);
    setlength(Script, len);
    fileseek(h, 0, 0);
    fileread(h, Script[1], len);
    fileclose(h);
    Script := LowerCase(Script);
    //writeln(script);
    lual_loadbuffer(Lua_script, @script[1], length(script), 'code');
    lua_pcall(Lua_script, 0, 0, 0);
    //lua_dofile(Lua_script,pchar(filename[1]));
    if functionname <> nil then
    begin
      lua_getglobal(Lua_script, functionname);
      Result := lua_pcall(Lua_script, 0, 1, 0);
    end;
    //writeln(result);
  end;
end;

function TScript.ExecScript(Pakname, filename, functionname: PChar): integer;
var
  Script: AnsiString;
  //Data: string;
  Data: TStringList;
  h: integer;
  len: integer;
begin
  if PakFile.ReadFromFileToBuffer(PakName, Filename) > 0 then
  begin
    setlength(Script, PakFile.bufferlen);
    Move(PakFile.databuffer[0], Script[1], PakFile.bufferlen);
    Script := LowerCase(Script);
    //writeln(script);
    lual_loadbuffer(Lua_script, @script[1], length(script), 'code');
    lua_pcall(Lua_script, 0, 0, 0);
    //lua_dofile(Lua_script,pchar(filename[1]));
    if functionname <> nil then
    begin
      lua_getglobal(Lua_script, functionname);
      Result := lua_pcall(Lua_script, 0, 1, 0);
    end;
    //writeln(result);
  end;
end;

end.
